package com.jz.yanshe.ui.fragment;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.aspsine.swipetoloadlayout.OnLoadMoreListener;
import com.aspsine.swipetoloadlayout.OnRefreshListener;
import com.aspsine.swipetoloadlayout.SwipeToLoadLayout;
import com.jz.yanshe.adapter.OrderAdapter;
import com.jz.yanshe.adapter.OrderWrapper;
import com.jz.yanshe.util.PreferencesHelper;

import com.jz.yanshe.base.BaseFragment;
import com.jz.yanshe.eventbus.OrderRefreshEvent;
import com.jz.yanshe.ui.UploadOrderFileActivity;

import com.jz.jd.rest.business.RestProxy;
import com.jz.jd.rest.exception.BusinessException;
import com.jz.jd.rest.response.OrderListResponse;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import com.jz.yanshe.R;
/**
 * Created by Administrator on 2018/1/10.
 */

public class OrderListFragment extends BaseFragment implements OnRefreshListener, OnLoadMoreListener {
    @BindView(R.id.swipe_target)
    RecyclerView recyclerView;
    @BindView(R.id.swipeToLoadLayout)
    SwipeToLoadLayout swipeToLoadLayout;

    @BindView(R.id.parent_container)
    LinearLayout parent_container;

    String orderid;

    List list = new ArrayList<OrderWrapper>();
    private String type = "";

    OrderAdapter myAdapter;

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_order_list;
    }

    @Override
    protected void initView() {
        recyclerView.setAdapter(myAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
                super.onDraw(c, parent, state);
            }

            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.bottom = 1;
            }
        });
        swipeToLoadLayout.setOnRefreshListener(this);
        swipeToLoadLayout.setOnLoadMoreListener(this);
        swipeToLoadLayout.setLoadMoreEnabled(false);


    }

    @Override
    protected void initData() {
        type = "0";
        type = getArguments().getString("TYPE");
        getAllOrderList(type);


        myAdapter = new OrderAdapter(getActivity(), list,parent_container);
        myAdapter.refreshData = new OrderAdapter.RefreshData() {
            @Override
            public void refreshData() {
                getAllOrderList(type);
            }

            @Override
            public void openGallery(String order_id,String pay_price) {
                orderid = order_id;
                Bundle bundle = new Bundle();
                bundle.putString("orderid", orderid);
                bundle.putString("pay_price",pay_price);
                startActivity(UploadOrderFileActivity.class, bundle);
            }
        };
//        getList(typeToString(type));
    }

    public static OrderListFragment newInstance(String type) {
        Bundle args = new Bundle();
        args.putString("TYPE", type);
        OrderListFragment fragment = new OrderListFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onLoadMore() {

    }

    @Override
    public void onRefresh() {
        getAllOrderList(type);
    }


    @Override
    public void onResume() {
        System.out.println("onResume");
        super.onResume();
    }


    private void getAllOrderList(String order_state) {
        RestProxy.getInstance().getAllOrderList(PreferencesHelper.getInstance().getString(getActivity(), "token"), order_state, new Observer<OrderListResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(OrderListResponse orderListResponse) {
                list.clear();
                for (int i = 0; i < orderListResponse.listM.size(); i++) {
                    OrderWrapper w1 = new OrderWrapper();
                    w1.item_type = OrderAdapter.ITEM_TYPE.ITEM_HEAD.ordinal();
                    w1.order_state = orderListResponse.listM.get(i).order_state;
                    w1.order_code = orderListResponse.listM.get(i).order_code;
                    w1.add_time = orderListResponse.listM.get(i).add_time;
                    list.add(w1);
                    for (int j = 0; j < orderListResponse.listM.get(i).goodsInfos.size(); j++) {
                        OrderWrapper w2 = new OrderWrapper();
                        w2.item_type = OrderAdapter.ITEM_TYPE.ITEM_BODY.ordinal();
                        w2.goods_name = orderListResponse.listM.get(i).goodsInfos.get(j).goods_name;
                        w2.style_color = orderListResponse.listM.get(i).goodsInfos.get(j).style_color;
                        w2.actual_price = orderListResponse.listM.get(i).goodsInfos.get(j).actual_price;
                        w2.style_size = orderListResponse.listM.get(i).goodsInfos.get(j).style_size;
                        w2.goods_num = orderListResponse.listM.get(i).goodsInfos.get(j).goods_num;
                        w2.goods_icon = orderListResponse.listM.get(i).goodsInfos.get(j).goods_icon;
                        w2.order_id = orderListResponse.listM.get(i).order_id;
                        list.add(w2);
                    }
                    OrderWrapper w3 = new OrderWrapper();
                    w3.order_state = orderListResponse.listM.get(i).order_state;
                    w3.item_type = OrderAdapter.ITEM_TYPE.ITEM_FOOT.ordinal();
                    w3.total_price = orderListResponse.listM.get(i).total_price;
                    w3.goods_total_num = orderListResponse.listM.get(i).goods_total_num;
                    w3.order_id = orderListResponse.listM.get(i).order_id;
                    w3.pay_price = orderListResponse.listM.get(i).pay_price;
                    list.add(w3);
                }
                myAdapter.notifyDataSetChanged();

            }

            @Override
            public void onError(Throwable e) {
                if (e instanceof BusinessException) {
                    toLogin(((BusinessException) e).errorCode);
                }
            }

            @Override
            public void onComplete() {
                swipeToLoadLayout.setRefreshing(false);
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }



    @Override
    public void onStop() {
        EventBus.getDefault().unregister(this);
        super.onStop();
    }


    @Subscribe(sticky = true)
    public void onMessageEvent(OrderRefreshEvent event) {
        if (event.message.equals("1")) {
            System.out.println(type + "  type");
            getAllOrderList(type);
        }
    }


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()) {
            if(!type .equals("")){
                getAllOrderList(type);
            System.out.println("可见" + type);}
        } else {
            System.out.println("不可见");
        }
    }

}
